package cn.stephen.study.demoproject.controller;

import cn.stephen.study.demoproject.dao.dbtwo.SBJCYJMapper;
import cn.stephen.study.demoproject.entity.SBJCYJ;
import cn.stephen.study.demoproject.service.SBJCYJService;
import cn.stephen.study.demoproject.util.PageUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/alarm")
public class AlarmController {
    @Autowired
    private SBJCYJService sbjcyjService;
    @Autowired
    private SBJCYJMapper sbjcyjMapper;
    @RequestMapping(value = "findAll")
    @ResponseBody
    public JSONObject findAll(@RequestParam("type") Integer type,
                              @RequestParam("startTime") String startTime,
                              @RequestParam("endTime") String endTime,
                              @RequestParam("warningType") Integer warningType,
                              @RequestParam("id") String id,
                              @RequestParam("page") Integer page,
                              @RequestParam("limit") Integer limit
    ) {
        JSONObject jo = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            List<SBJCYJ> alls = sbjcyjService.findAlls(id, type, startTime, endTime, warningType);
            List<SBJCYJ> sbjcyj = PageUtil.listPaging(alls, page, limit);
            JSONArray jsonArray = new JSONArray();
            for (SBJCYJ sbjcyj1 : sbjcyj) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("time", sbjcyj1.getSBJCYJ060() == null ? "" : sdf.format(sbjcyj1.getSBJCYJ060()));//预警时间
                jsonObject.put("type", 0);
                jsonObject.put("id", sbjcyj1.getSBJCYJ010());//id
                jsonObject.put("deviceCode", sbjcyj1.getSBJCYJ020());//设备编号
                jsonObject.put("type2", sbjcyj1.getSBJCYJ030());//设备预警类型
                jsonObject.put("number", sbjcyj1.getSBJCYJ040());//设备数值
                jsonObject.put("state", sbjcyj1.getSBJCYJ050());//预警级别
                jsonObject.put("deviceName", sbjcyj1.getSBJCYJ070());//设备名称
                jsonObject.put("code", sbjcyj1.getSBJCYJ080());//网点编号
                jsonObject.put("name", sbjcyj1.getSBJCYJ090());//网点名称
                jsonObject.put("dataCode", sbjcyj1.getSBJCYJ100());//监测指标
                jsonObject.put("news", sbjcyj1.getSBJCYJ110());//预警详细信息
                jsonObject.put("progresType", sbjcyj1.getSBJCYJ120());//传感器级别
                jsonObject.put("status", sbjcyj1.getSTATUS());//报警处理状态
                jsonArray.add(jsonObject);
            }
            jo.put("data", jsonArray);
            jo.put("total", alls == null ? 0 : alls.size());
            jo.put("code", 0);
            jo.put("msg", "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            jo.put("data", null);
            jo.put("total", 10);
            jo.put("code", 0);
            jo.put("msg", "");
        }
        return jo;
    }

    @RequestMapping(value = "getDetail")
    @ResponseBody
    public JSONObject findAll(@RequestParam("id") String id
    ) {
        JSONObject jo = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
                SBJCYJ sbjcyj1 = sbjcyjMapper.getDetail(id);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("time", sbjcyj1.getSBJCYJ060() == null ? "" : sdf.format(sbjcyj1.getSBJCYJ060()));//预警时间
                jsonObject.put("type", 0);
                jsonObject.put("id", sbjcyj1.getSBJCYJ010());//id
                jsonObject.put("deviceCode", sbjcyj1.getSBJCYJ020());//设备编号
                jsonObject.put("type2", sbjcyj1.getSBJCYJ030());//设备预警类型
                jsonObject.put("number", sbjcyj1.getSBJCYJ040());//设备数值
                jsonObject.put("state", sbjcyj1.getSBJCYJ050());//预警级别
                jsonObject.put("deviceName", sbjcyj1.getSBJCYJ070());//设备名称
                jsonObject.put("code", sbjcyj1.getSBJCYJ080());//网点编号
                jsonObject.put("name", sbjcyj1.getSBJCYJ090());//网点名称
                jsonObject.put("dataCode", sbjcyj1.getSBJCYJ100());//监测指标
                jsonObject.put("news", sbjcyj1.getSBJCYJ110());//预警详细信息
                jsonObject.put("progresType", sbjcyj1.getSBJCYJ120());//传感器级别
                jsonObject.put("status", sbjcyj1.getSTATUS());//报警处理状态
            JSONArray jsonArray=new JSONArray();
            jsonArray.add(jsonObject);
            jo.put("data", jsonArray);
            jo.put("code", 0);
            jo.put("msg", "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            jo.put("data", null);
            jo.put("total", 10);
            jo.put("code", 0);
            jo.put("msg", "");
        }
        return jo;
    }
    /**
     * 填报
     */
    @GetMapping("submit")
    @ResponseBody
    public JSONObject insertSbJCYJSBUMIT(@RequestParam("SBJCYJ010") String SBJCYJ010,
                                         @RequestParam("SBUMITOR") String SBUMITOR,
                                         @RequestParam("SBUMITRESULT") String SBUMITRESULT){
       JSONObject jo = new JSONObject();
        try {
            sbjcyjMapper.insertSbJCYJSBUMIT(SBJCYJ010,SBUMITOR,new Date(),SBUMITRESULT);
            sbjcyjMapper.updateStatus(SBJCYJ010);
            jo.put("code", 1);
            jo.put("msg", "填报成功");
        } catch (Exception e) {
            e.printStackTrace();
            jo.put("code", 0);
            jo.put("msg", "填报失败");
        }
        return jo;
    }




}
